Ext.define('Admin.view.leave.LeaveViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.leave',
    onClickLeaveGridAddButton: function (view) {
    		Ext.create(Ext.apply({xtype: 'leave-add-window'}));
    },
    onClickAddFormSubmitButton: function (btn) {
    	var form = btn.up('form');
		form.getForm().submit({       
			url:'leave/saveOrUpdate',
			success: function(form, action){    
				Ext.Msg.alert('Success', action.result.msg,function(){
					btn.up('window').close();
					form.getViewModel().getStore('leaveGridStore').reload();
				});       
			},       
			failure: function(form, action){
				Ext.Msg.alert('Error', action.result.msg);
			}
		});
    },
	onClickLeaveGridUpdateButton: function (btn) {
      alert("on Click Update Button!");
    },
	onClickLeaveGridDeleteButton: function (btn) {
				var grid = btn.up('gridpanel');
				var selModel = grid.getSelectionModel();
                if (selModel.hasSelection()) {
                    Ext.Msg.confirm("警告", "确定要删除吗？", function (button) {
                        if (button == "yes") {
                            var selected = selModel.getSelection();
                            var selectIds = []; //要删除的id
                            Ext.each(selected, function (item) {
                                selectIds.push(item.data.id);
                            })
                          	Ext.Ajax.request({ 
								url : 'leave/delete', 
								method : 'post',
								params : { 
									//ids[] :selectIds
									ids :selectIds
								}, 
								success: function(response, options) {
					                var json = Ext.util.JSON.decode(response.responseText);
						            if(json.success){
						            	Ext.Msg.alert('操作成功', json.msg, function() {
						                    grid.getStore().reload();
						                });
							        }else{
							        	 Ext.Msg.alert('操作失败', json.msg);
							        }
					            }
							});

                        }
                    });
                }
                else {
                    Ext.Msg.alert("错误", "没有任何行被选中，无法进行删除操作！");
                }
    }
   
	,onClickStartButton: function (btn) {
		
			var gridPanel = btn.up('gridpanel');
			var record = gridPanel.getSelectionModel().getLastSelected();
			alert("id:"+record.get('id'));
			Ext.Ajax.request({ 
			url : 'leave/start', 
			method : 'post', 
			params : { 
				id :record.get('id')//record.data.id
			}, 
			success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
	            if(json.success){
	            	Ext.Msg.alert('操作成功', json.msg, function() {
	                    gridPanel.getStore().reload();
	                });
		        }else{
		        	 Ext.Msg.alert('操作失败', json.msg);
		        }
            }
		});
    }
});
